package planit.view;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Event;
import planit.model.Model;
import planit.view.wrappers.EventWrapper;
import planit.view.wrappers.ModelCellRenderer;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class DeleteEventDialog extends javax.swing.JDialog {
	private JLabel selectLabel;
	private JList jList1;
	private JLabel timeLabel;
	private JLabel dateLabel;
	private JLabel descriptionLabel;
	private JButton cancelButton;
	private JLabel nameLabel;
	private JButton deleteButton;
	private JTextPane descriptionPane;
	private JTextPane namePane;
	private AbstractAction cancelAction;

	private Model model;

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
			.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Auto-generated main method to display this JDialog
	 */

	public DeleteEventDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updateEventList();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setPreferredSize(new java.awt.Dimension(350, 368));
				{
					selectLabel = new JLabel();
					getContentPane().add(selectLabel);
					selectLabel.setText("Select a Event");
					selectLabel.setBounds(6, 6, 77, 16);
				}
				{
					ListModel jList1Model = new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
					jList1 = new JList();
					getContentPane().add(jList1);
					jList1.setModel(jList1Model);
					jList1.setBounds(6, 28, 115, 251);
					jList1
					.addListSelectionListener(new ListSelectionListener() {
						public void valueChanged(ListSelectionEvent evt) {
							jList1ValueChanged(evt);
						}
					});
				}
				{
					deleteButton = new JButton();
					getContentPane().add(deleteButton);
					deleteButton.setAction(ControllerFactory
							.getController(ControllerType.DELETE_EVENT));
					deleteButton.getAction().putValue("DeleteEventDialog", this);
					deleteButton.setText("Delete");
					deleteButton.setBounds(6, 285, 115, 39);
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Event name:");
					nameLabel.setBounds(127, 12, 73, 16);
				}
				{
					descriptionLabel = new JLabel();
					getContentPane().add(descriptionLabel);
					descriptionLabel.setText("Description:");
					descriptionLabel.setBounds(127, 64, 111, 19);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					getContentPane().add(getNamePane());
					getContentPane().add(getDescriptionPane());
					getContentPane().add(getDateLabel());
					getContentPane().add(getTimeLabel());
					cancelButton.setAction(getCancelAction());
					cancelButton.setText("Cancel");
					cancelButton.setBounds(219, 285, 109, 39);
				}
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jList1ValueChanged(ListSelectionEvent evt) {
		if (jList1.getSelectedValue() == null) {
			namePane.setText("");
			descriptionPane.setText("");
			dateLabel.setText("Date: ");
			timeLabel.setText("Time: ");
		} else {
			Event temp;
			temp = model.getSchedule().getEvent(((EventWrapper)jList1.getSelectedValue()).getId());
			namePane.setText(temp.getName());
			descriptionPane.setText(temp.getDescription());
			String str = temp.getStartTime().toString();
			dateLabel.setText("Date: " + str.substring(0, str.length() -18)
					+ " - "
					+ str.substring(str.length() - 4));
			timeLabel.setText("Time: " 
					+ str.substring(str.length() - 18,
							str.length() - 5));
		}
	}

	public void updateEventList() {
		Event[] event = new Event[0];
		ArrayList<JLabel> allEvents = new ArrayList<JLabel>();
		event = model.getSchedule().getEvents().toArray(event);
		for (int i = 0; i < event.length; i++) {
			allEvents.add(new EventWrapper(event[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allEvents.toArray());
		jList1.setModel(list2Model);
		jList1.setCellRenderer(new ModelCellRenderer());
	}

	private JTextPane getNamePane() {
		if(namePane == null) {
			namePane = new JTextPane();
			namePane.setBounds(127, 34, 201, 24);
			namePane.setEditable(false);
		}
		return namePane;
	}

	private JTextPane getDescriptionPane() {
		if(descriptionPane == null) {
			descriptionPane = new JTextPane();
			descriptionPane.setBounds(127, 83, 201, 151);
			descriptionPane.setEditable(false);
		}
		return descriptionPane;
	}

	private JLabel getDateLabel() {
		if(dateLabel == null) {
			dateLabel = new JLabel();
			dateLabel.setText("Date: ");
			dateLabel.setBounds(127, 246, 201, 16);
		}
		return dateLabel;
	}

	private JLabel getTimeLabel() {
		if(timeLabel == null) {
			timeLabel = new JLabel();
			timeLabel.setText("Time: ");
			timeLabel.setBounds(127, 268, 201, 16);
		}
		return timeLabel;
	}

	public int getEventId() {
		return ((EventWrapper)jList1.getSelectedValue()).getId();
	}

	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
}
